<?php
class LoginController extends Controller {
	
	public function indexAction() {
        if($this->_session->isLogged()) {
            $this->redirect('leks');
        }
        $this->setErrors();
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Login',
			'panel' => false,
            'pageId' => 'login'
		));
		$this->_view->render('login/index');
	}
	
	public function loginAction() {
        if($this->_session->isLogged()) {
            $this->redirect('leks');
        }
		if($this->_request->getString('doLogin')) {
			$login = $this->_request->getString('login');
			$password = $this->_request->getString('password');
			$loggedUser = $this->_model->doLogin($login, $password);
			if(is_object($loggedUser)) {
				$this->_session->set('isLogged', true);
				$this->_session->set('currentUser', serialize($loggedUser));
				if(!$loggedUser->isRoot()) {
					$this->redirect('website');
				}
				else {
					$this->redirect('index');
				}
			}
			else {
				$this->addError('Wrong login data!');
                $this->delegateToAction('index');
			}
		}
	}
	
	public function logoutAction() {
        $this->checkLoginRedirect();
		$user = $this->_session->getCurrent();
		//$user->setLastLogin();
		$this->_session->logout();
		$this->redirect('index');
	}
}